package com.hanson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsTypeBean;
import com.hanson.entity.UserBean;
import com.hanson.service.GoodsTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/goodsType")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap, String pid, HttpServletRequest request) {
        modelMap.put("cateName", goodsTypeService.selecRootType(0));
        if (StringUtils.isNotEmpty(pid)) {
            modelMap.put("pid", Integer.parseInt(pid));
        } else {
            modelMap.put("pid", 0);
        }
        String token = BusinessUtil.getCookieToken(request, "token");
        if (!org.springframework.util.StringUtils.isEmpty(token)) {
            UserBean userBean = (UserBean) redisService.get("LOGIN:" + token);
            if (userBean != null && null != userBean.getId()) {
                modelMap.put("userType",userBean.getUserType());
            }
        }
        return "admin/goods/goodsType/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap modelMap, String id) {
        List<GoodsTypeBean> goodsTypeBeanList = goodsTypeService.selecRootType(0);
        List<JSONObject> options = new ArrayList<>();
        JSONObject rootObject = new JSONObject();
        rootObject.put("label", "顶级菜单");
        rootObject.put("value", 0);
        options.add(rootObject);
        if (!CollectionUtils.isEmpty(goodsTypeBeanList)) {
            goodsTypeBeanList.stream().forEach(c -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("label", "|-----" + c.getCateName());
                jsonObject.put("value", c.getId());
                options.add(jsonObject);
            });
        }
        modelMap.put("options", JSON.toJSONString(options));
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", JSON.toJSONString(goodsTypeService.detailGoodsType(Integer.parseInt(id))));
        }
        return "admin/goods/goodsType/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(GoodsTypeBean goodsTypeBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsTypeService.addGoodsType(goodsTypeBean);
        resultMessage.setMsg("新增商品分类成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(GoodsTypeBean goodsTypeBean) {
        ResultMessage resultMessage = new ResultMessage();
        goodsTypeService.modifyGoodsType(goodsTypeBean);
        resultMessage.setMsg("修改商品分类成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        goodsTypeService.deleteGoodsType(Integer.parseInt(id));
        resultMessage.setMsg("删除商品分类成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/setShow/{id}/{isShow}", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage setShow(@PathVariable Integer id, @PathVariable Integer isShow) {
        ResultMessage resultMessage = new ResultMessage();
        goodsTypeService.setIsShow(id, isShow);
        resultMessage.setMsg("设置是否显示成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<GoodsTypeBean> listData(int limit, int page, GoodsTypeBean goodsTypeBean) {
        Page<GoodsTypeBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(goodsTypeService.listGoodsType(pageInfo, goodsTypeBean));
    }

}
